package com.dzl.order.utils;

import com.dzl.constant.WelfareOrderEnum;
import com.dzl.order.pojo.WelfareOrder;
import com.dzl.order.pojo.WelfareOrderAddress;
import com.dzl.order.pojo.dto.WelfareOrderAddressDto;
import com.dzl.order.pojo.dto.WelfareOrderDto;
import com.dzl.userwx.pojo.WelfareUserAddress;
import com.dzl.util.EmptyUtils;
import com.dzl.util.StringUtil;

import java.util.Date;
import java.util.Map;

/**
 * 订单操作工具类
 *
 * @author ChenLong
 * @date 2021-08-06 14:51:16
 */
public class OrderUtils {
    /**
     * 校验用户地址函数
     *
     * @param welfareOrderAddressDto 用户地址对象
     * @param resMap                 校验map
     * @return boolean
     * @author ChenLong
     * @date 2021-06-06 12:36:23
     **/
    public static boolean validUserOrderAddress(WelfareOrderAddressDto welfareOrderAddressDto, Map<String, Object> resMap) {
        if (EmptyUtils.isEmpty(welfareOrderAddressDto.getPhone())) {
            resMap.put("code", 1802);
            resMap.put("msg", "请输入联系方式");
            return true;
        }
        if (EmptyUtils.isEmpty(welfareOrderAddressDto.getProvince())) {
            resMap.put("code", 1803);
            resMap.put("msg", "请输入省份");
            return true;
        }
        if (EmptyUtils.isEmpty(welfareOrderAddressDto.getAddress())) {
            resMap.put("code", 1804);
            resMap.put("msg", "请输入详细地址");
            return true;
        }
        return false;
    }

    /**
     * 构建用户订单地址参数
     *
     * @param welfareOrderAddressDto 订单地址dto
     * @return com.dzl.order.pojo.WelfareOrderAddress
     * @author ChenLong
     * @date 2021-06-06 12:36:31
     **/
    public static WelfareUserAddress buildUserAddressParams(WelfareOrderAddressDto welfareOrderAddressDto, Integer userId) {
        WelfareUserAddress welfareUserAddress = new WelfareUserAddress();
        welfareUserAddress.setUserId(userId);
        welfareUserAddress.setName(welfareOrderAddressDto.getName());
        welfareUserAddress.setCreateTime(new Date());
        welfareUserAddress.setStatus(1);
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getPhone())) {
            welfareUserAddress.setPhone(welfareOrderAddressDto.getPhone());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getProvince())) {
            welfareUserAddress.setProvince(welfareOrderAddressDto.getProvince());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getAddress())) {
            welfareUserAddress.setAddress(welfareOrderAddressDto.getAddress());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getCity())) {
            welfareUserAddress.setCity(welfareOrderAddressDto.getCity());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getArea())) {
            welfareUserAddress.setArea(welfareOrderAddressDto.getArea());
        }
        return welfareUserAddress;
    }

    /**
     * 构建用户订单地址参数
     *
     * @param welfareOrderAddressDto 订单地址dto
     * @return com.dzl.order.pojo.WelfareOrderAddress
     * @author ChenLong
     * @date 2021-06-06 12:36:31
     **/
    public static WelfareOrderAddress buildUserOrderAddressParams(WelfareOrderAddressDto welfareOrderAddressDto) {
        WelfareOrderAddress welfareOrderAddress = new WelfareOrderAddress();
        welfareOrderAddress.setName(welfareOrderAddressDto.getName());
        welfareOrderAddress.setCreateTime(new Date());
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getPhone())) {
            welfareOrderAddress.setPhone(welfareOrderAddressDto.getPhone());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getProvince())) {
            welfareOrderAddress.setProvince(welfareOrderAddressDto.getProvince());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getAddress())) {
            welfareOrderAddress.setAddress(welfareOrderAddressDto.getAddress());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getCity())) {
            welfareOrderAddress.setCity(welfareOrderAddressDto.getCity());
        }
        if (EmptyUtils.isNotEmpty(welfareOrderAddressDto.getArea())) {
            welfareOrderAddress.setArea(welfareOrderAddressDto.getArea());
        }
        return welfareOrderAddress;
    }

    /**
     * 构建Order对象
     *
     * @author ChenLong
     * @date 2021-08-06 15:06:01
     * @param welfareOrder Order对象
     * @param orderDto 提交订单参数
     * @return void
     **/
    public static void buildOrder(WelfareOrder welfareOrder,WelfareOrderDto orderDto){
        welfareOrder.setOrderNo(StringUtil.generateOrderNo());
        welfareOrder.setOrderType(orderDto.getOrderType());
        welfareOrder.setCreateTime(new Date());
        // TODO 微信支付流水号
        // welfareOrder.setOutTradeNo(StringUtil.getBillNumber());
        welfareOrder.setOrderStatus(WelfareOrderEnum.ORDER_STATUS_UNPAY.getCode());
        welfareOrder.setUserId(orderDto.getUserId());
        welfareOrder.setEnterpriseId(orderDto.getEnterpriseId());
    }
}
